package com.roncoo.education.course.service.auth;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.roncoo.education.common.config.ThreadContext;
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreReport;
import com.roncoo.education.course.service.auth.biz.AuthExperimentScoreBiz;
import com.roncoo.education.course.service.auth.req.AuthExperimentScoreReq;
import com.roncoo.education.course.service.auth.resp.AuthExperimentScoreResp;
import com.roncoo.education.course.service.auth.resp.AuthExperimentScoreStepResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * AUTH-实验分数
 *
 * @author wujing
 * @date 2024-03-15
 */
@Api(tags = "AUTH-实验分数")
@RestController
@RequiredArgsConstructor
@RequestMapping("/course/auth/experiment/score")
public class AuthExperimentScoreController {

    @NotNull
    private final AuthExperimentScoreBiz biz;

    @ApiOperation(value = "实验管理分页", notes = "根据条件进行实验分页")
    @RequestMapping(value = "/page", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Page<AuthExperimentScoreResp>> listForPage(@RequestBody AuthExperimentScoreReq req) {
        return biz.listForPage(req, ThreadContext.userId());
    }
    
    @ApiOperation(value = "实验管理分页", notes = "根据条件进行实验分页")
    @RequestMapping(value = "/listSteps", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<AuthExperimentScoreStepResp>> listSteps(@RequestBody AuthExperimentScoreReq req) {
        return biz.listSteps(req);
    }
    
    @RequestMapping("/download/{reportId}")
    public void download(@PathVariable long reportId, HttpServletResponse response) throws IOException {
    	ExperimentScoreReport report =  biz.getById(reportId);
    	if (report == null || StringUtil.isBlank(report.getFilename()) || StringUtil.isBlank(report.getContents())) {
    		return;
    	}
    	
        // 读取文件内容到字节数组
        byte[] data = report.getContents().getBytes();
//        // 设置响应头，指定文件名
//        response.reset();
//        response.setHeader("Content-Disposition", "attachment; filename=" + report.getFilename());
//        response.addHeader("Content-Length", "" + data.length);
//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        response.getOutputStream().write(data);
        
		StringBuilder content = new StringBuilder();
        OutputStream outputStream = null;
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + report.getFilename());
            response.addHeader("Content-Length", "" + data.length);
            response.setHeader("Content-Type", "application/octet-stream");
            response.setContentType("application/octet-stream; charset=UTF-8");
            outputStream = response.getOutputStream();
            outputStream.write(data);
            outputStream.flush();
        } catch (Exception e) {
            // ignored
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
